package com.download.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.download.web.entity.BusAdvertising;
import com.download.web.entity.model.Page;
import com.download.web.entity.model.Result;
import com.download.web.mapper.AdvertisingMapper;
import com.download.web.service.IAdvertisingService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AdvertisingServiceImpl extends ServiceImpl<AdvertisingMapper, BusAdvertising> implements IAdvertisingService {

    @Resource
    private AdvertisingMapper advertisingMapper;

    @Override
    public Page<BusAdvertising> advertisingPages(Integer pageIndex, Integer pageSize) {
        List<BusAdvertising> advertisingList=advertisingMapper.advertisingList(pageIndex*pageSize,pageSize);
        return new Page<>(advertisingList,advertisingMapper.advertisingCount(),pageIndex,pageSize);
    }

    @Override
    public Result saveAdvertising(BusAdvertising advertising) {
        BusAdvertising advertisingCheck=getOne(new QueryWrapper<BusAdvertising>().eq("position",advertising.getPosition()));
        if (advertisingCheck!=null){
            return Result.error("这个广告位已经有广告了");
        }
        save(advertising);
        return Result.ok(advertising);
    }

    @Override
    public Result updateAdvertising(BusAdvertising advertising) {
        BusAdvertising advertisingCheck=getOne(new QueryWrapper<BusAdvertising>().eq("position",advertising.getPosition()));
        if (advertisingCheck!=null&&!advertising.getId().equals(advertisingCheck.getId())){
            return Result.error("这个广告位已经有广告了");
        }
        updateById(advertising);
        return Result.ok();
    }
}
